#ifndef __PROCESS__POLL__HPP__
#define __PROCESS__POLL__HPP__

#include <iostream>
#include <vector>
#include <unistd.h>
#include <cstdlib>
#include <string>
#include <sys/wait.h>
#include "Task.hpp"

class Channel
{
public:
    Channel(pid_t id, int fd) : _subid(id), _wfd(fd)
    {
        std::cout << "我是子进程, 我的pid是:  " << std::to_string(_subid) << " 我的fd是:  " << std::to_string(_wfd) << std::endl;
    }
    ~Channel()
    {
    }
    int GetFd()
    {
        return _wfd;
    }
    int GetSubid()
    {
        return _subid;
    }
    void Send(int code)
    {
        std::cout << "向子进程 [" << _subid << "] 派发任务,任务码是: " << code << std::endl;
        int n = write(_wfd, &code, sizeof(code));
    }

    void Close()
    {
        close(_wfd);
    }
    void Wait()
    {
        waitpid(_subid, nullptr, 0);
    }

private:
    pid_t _subid;
    int _wfd;
    std::string _name;
};

class ChannelManager
{
public:
    ChannelManager() : _next(0)
    {
    }
    ~ChannelManager()
    {
    }
    void Push(pid_t id, int fd)
    {
        _vc.emplace_back(id, fd); // emplace_back会通过传参直接调用Channel符合id,fd的构造函数
    }
    Channel SelectChannel()
    {
        return _vc[(_next++) % _vc.size()];
    }

    void StopChannel()
    {
        for (auto &c : _vc)
        {
            c.Close();
        }
    }
    void WaitChannel()
    {
        for (auto &c : _vc)
        {
            c.Wait();
        }
    }
    void CloseAll()
    {
        for (auto &c : _vc)
        {
            c.Close();
        }
    }

private:
    std::vector<Channel> _vc;
    int _next;
};

const int PNUM = 5;

class ProcessPoll
{
public:
    ProcessPoll(int num = PNUM) : _process_num(num)
    {
        _tm.Push(PrintTask);
        _tm.Push(DowlanTask);
        _tm.Push(ReadTask);
    }
    ~ProcessPoll()
    {
    }

    void Work(int fd)
    {
        while (true)
        {
            int code = 0;
            ssize_t s = read(fd, &code, sizeof(code));
            if (s == sizeof(code))
            {
                _tm.Extrue(code);
            }
            else if (s == 0)
            {
                return;
            }
            else
            {
                continue;
            }
        }
    }

    void start()
    {

        for (int i = 0; i < _process_num; i++)
        {
            // 1.创建管道
            int pipefd[2] = {0};
            if (pipe(pipefd))
            {
                perror("pipe");
                exit(1);
            }
            // 2.创建子进程
            pid_t pid = fork();
            if (pid == 0)
            {
                // 子进程
                _cm.CloseAll();
                close(pipefd[1]);
                Work(pipefd[0]);
                close(pipefd[0]);
                exit(0);
            }
            else if (pid < 0)
            {
                perror("fork");
                exit(2);
            }
            // 父进程
            _cm.Push(pid, pipefd[1]);
            close(pipefd[0]);
        }
    }

    void Run()
    {
        while (true)
        {
            // 1.获取任务
            int code = _tm.GetTaskCode();

            // 2.选择子进程
            auto c = _cm.SelectChannel();

            // 3.派发任务
            c.Send(code);
            sleep(1);
        }
    }

    void Stop()
    {
        _cm.StopChannel();
        _cm.WaitChannel();
    }

private:
    ChannelManager _cm;
    int _process_num;
    TaskManager _tm;
};

#endif
